#!usr/bin/env python  
# -*- coding:utf-8 _*-
""" 
@author:robot
@file: demo.py 
@version:
@time: 2022/06/22 
"""

import socket
import time

MaxBytes = 1024 * 1024

server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server.settimeout(60)
host = '127.0.0.1'
port = 11223
server.bind((host, port))  # 绑定端口

# client
serve_address = ('127.0.0.1', 11224)

client2 = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client2.settimeout(60)

server.listen(5)  # 监听
try:
    client, addr = server.accept()  # 等待客户端连接
    print(addr, " 连接上了")
    while True:
        try:
            data = client.recv(MaxBytes)
            if not data:
                pass
            else:
                print(data)
                client2.connect_ex(serve_address)
                client2.send(data)
                client2.close()
        except Exception:
            client, addr = server.accept()  # 等待客户端连接
except Exception as e:
    print("出现异常：")
    print(repr(e))
finally:
    server.close()  # 关闭连接
